<template>
  <div class="change_admin">
    <el-dialog
      :title="title"
      class="gl_dialog_TitleLine"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="880px"
    >
      <el-form ref="kyorugiSettingForm" class="gl_form_mini" :model="SiSTKWEventsManagementKyorugiSettingDto" :rules="CompetitiveDataVO_Rules" label-width="140px" label-position="right" style="padding-right: 30px;">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="名称" prop="name" style="height:32px; box-sizing:border-box">
              <template v-if="!isCheck">
                <el-input v-model="SiSTKWEventsManagementKyorugiSettingDto.name" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.name }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身体阈值" prop="bodyThreshold">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.bodyThreshold" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.bodyThreshold }}</span>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="头部阈值" prop="headThreshold">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.headThreshold" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.headThreshold }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大犯规" prop="maxFoul">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.maxFoul" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.maxFoul }}</span>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <template v-if="code === 'team'">
            <el-col :span="12">
              <el-form-item label="最大分差" prop="maxDiffScore">
                <template v-if="!isCheck">
                  <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.maxDiffScore" :min="0" style="width:100%" size="mini" clearable />
                </template>
                <template v-else>
                  <span>{{ SiSTKWEventsManagementKyorugiSettingDto.maxDiffScore }}</span>
                </template>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="12">
            <el-form-item label="青方审议" prop="gConsideration">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.gConsideration" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.gConsideration }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="红方审议" prop="rConsideration">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.rConsideration" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.rConsideration }}</span>
              </template>
            </el-form-item>
          </el-col>
          <template v-if="code === 'team'">
            <el-col :span="24">
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="回合数" prop="rounds">
                    <template v-if="!isCheck">
                      <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.rounds" :min="0" :max="10" style="width:100%" size="mini" clearable />
                    </template>
                    <template v-else>
                      <span>{{ SiSTKWEventsManagementKyorugiSettingDto.rounds }}</span>
                    </template>
                  </el-form-item>
                </el-col>
                <el-col :span="12" align="right">
                  <el-button size="mini" type="text" @click="showTeamRounds = !showTeamRounds">
                    <span class="gl_color_primary">{{ showTeamRounds ? '收起' : '展开' }}</span>
                  </el-button>
                </el-col>
              </el-row>
              <el-row v-show="showTeamRounds" :gutter="20" style="padding-left: 140px;">
                <el-col v-for="(item, index) in SiSTKWEventsManagementKyorugiSettingDto.rounds" :key="'rounds_' + index" :span="24" class="gl_mb_10">
                  <round-setting
                    :ref="'roundSetting_' + index"
                    :number="index + 1"
                    :round-setting="SiSTKWEventsManagementKyorugiSettingDto.roundSettings"
                    :type-code="typeCode"
                  />
                </el-col>
              </el-row>
            </el-col>
          </template>
          <template v-else>
            <el-col :span="12">
              <el-form-item label="回合数" prop="rounds">
                <template v-if="!isCheck">
                  <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.rounds" :min="0" :max="10" style="width:100%" size="mini" clearable />
                </template>
                <template v-else>
                  <span>{{ SiSTKWEventsManagementKyorugiSettingDto.rounds }}</span>
                </template>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="最大分差" prop="maxDiffScore">
                <template v-if="!isCheck">
                  <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.maxDiffScore" :min="0" style="width:100%" size="mini" clearable />
                </template>
                <template v-else>
                  <span>{{ SiSTKWEventsManagementKyorugiSettingDto.maxDiffScore }}</span>
                </template>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回合时间（分）" prop="roundMinutes">
                <template v-if="!isCheck">
                  <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.roundMinutes" :min="0" style="width:100%" size="mini" clearable />
                </template>
                <template v-else>
                  <span>{{ SiSTKWEventsManagementKyorugiSettingDto.roundMinutes }}</span>
                </template>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回合时间（秒）" prop="roundSeconds">
                <template v-if="!isCheck">
                  <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.roundSeconds" :min="0" :max="60" style="width:100%" size="mini" clearable />
                </template>
                <template v-else>
                  <span>{{ SiSTKWEventsManagementKyorugiSettingDto.roundSeconds }}</span>
                </template>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="12">
            <el-form-item label="休息时间（分）" prop="breakMinutes">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.breakMinutes" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.breakMinutes }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="休息时间（秒）" prop="breakSeconds">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.breakSeconds" :min="0" :max="60" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.breakSeconds }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计时时间（分）" prop="timingMinutes">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.timingMinutes" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.timingMinutes }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计时时间（秒）" prop="timingSeconds">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.timingSeconds" :min="0" :max="60" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.timingSeconds }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="加时赛时间（分）" prop="extraMinutes">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.extraMinutes" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.extraMinutes }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="加时赛时间（秒）" prop="extraSeconds">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.extraSeconds" :min="0" :max="60" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.extraSeconds }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="加时赛分数" prop="extraScore">
              <template v-if="!isCheck">
                <el-input-number v-model="SiSTKWEventsManagementKyorugiSettingDto.extraScore" :min="0" style="width:100%" size="mini" clearable />
              </template>
              <template v-else>
                <span>{{ SiSTKWEventsManagementKyorugiSettingDto.extraScore }}</span>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="close('form')">{{ $t('button.cancel') }}</el-button>
        <template v-if="!isCheck">
          <el-button type="primary" size="mini" @click="saveEdit('form')">{{ $t('button.save') }}</el-button>
        </template>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { SiSTKWEventsManagementKyorugiSettingDto } from '@/lib/form/game/swaggerForm';
import { CompetitiveDataVO_Rules } from '@/lib/validate/configuration/index.js';
import { CopyData } from '@/lib';
import roundSetting from './roundSetting';
export default {
  name: 'Account',
  components: {
    roundSetting
  },
  data() {
    return {
      SiSTKWEventsManagementKyorugiSettingDto: new SiSTKWEventsManagementKyorugiSettingDto(),
      CompetitiveDataVO_Rules: new CompetitiveDataVO_Rules(),
      title: '', // 弹框标题
      dialogVisible: false,
      currentData: [],
      row: 0,
      inputDataName: '',
      typeCode: 'add',
      code: 'ind',
      showTeamRounds: true
    };
  },
  computed: {
    isCheck: function() {
      return this.typeCode === 'check';
    }
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.$refs.kyorugiSettingForm.resetFields();
      }
    }
  },
  methods: {
    open(row, typeCode, code) {
      this.typeCode = typeCode;
      this.code = code;
      this.showTeamRounds = true;
      this.SiSTKWEventsManagementKyorugiSettingDto = new SiSTKWEventsManagementKyorugiSettingDto();
      if (typeCode === 'add') {
        this.title = `新增${code === 'ind' ? '个人' : '团体'}竞技模型`;
        this.title = code === 'base' ? '默认初始设置' : this.title;
      } else {
        this.title = typeCode === 'edit' ? `编辑${code === 'ind' ? '个人' : '团体'}竞技模型` : `${code === 'ind' ? '个人' : '团体'}竞技模型详情`;
        this.title = code === 'base' ? '默认初始设置' : this.title;
      }
      if (row) {
        this.SiSTKWEventsManagementKyorugiSettingDto = Object.assign({}, row);
        console.log(this.SiSTKWEventsManagementKyorugiSettingDto);
      }
      this.dialogVisible = true;
    },
    close() {
      this.$refs.kyorugiSettingForm.resetFields();
      this.dialogVisible = false;
    },
    // 点击确定
    saveEdit(formName) {
      const rounds = this.SiSTKWEventsManagementKyorugiSettingDto.rounds;
      const promiseArr = [this.valid()];
      if (this.code === 'team') {
        for (let i = 0; i < rounds; i++) {
          const _ref = this.$refs['roundSetting_' + i][0].valid();
          promiseArr.push(_ref);
        }
      }
      Promise.all(promiseArr).then(res => {
        const _data = res;
        const roundSettings = [];
        for (let i = 1; i < _data.length; i++) {
          if (_data[i].roundTypeCode === 'Normal') {
            _data[i].parts = [];
          }
          delete _data[i].roundType;
          roundSettings.push(_data[i]);
        }
        this.SiSTKWEventsManagementKyorugiSettingDto.roundSettings = roundSettings;
        const returnData = new SiSTKWEventsManagementKyorugiSettingDto();
        CopyData(this.SiSTKWEventsManagementKyorugiSettingDto, returnData);
        if (this.code === 'team') {
          returnData.isTeam = true;
        }
        if (this.typeCode === 'add') {
          if (this.code === 'base') {
            var baseData = JSON.parse(JSON.stringify(this.SiSTKWEventsManagementKyorugiSettingDto));
            this.$emit('updateBaseKyorugiSetting', baseData);
          } else {
            returnData.id = null;
            this.$emit('pushTabelData', returnData);
          }
        } else {
          var editData = JSON.parse(JSON.stringify(this.SiSTKWEventsManagementKyorugiSettingDto));
          this.$emit('editTabelData', editData);
        }
      });
    },
    valid() {
      return new Promise((resolve, reject) => {
        this.$refs.kyorugiSettingForm.validate((valid) => {
          if (valid) {
            resolve(this.SiSTKWEventsManagementKyorugiSettingDto);
          } else {
            reject(false);
          }
        });
      });
    }
  }
};
</script>
